﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.wjl;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.WJL;
using B.S.Sterilizationcenter.Read.API.Dto;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.WJL
{
    public class GetCleanwatchIdCommandHanders : IRequestHandler<GetCleanwatchIdCommands, ApiResult<List<CleanwatchItemDto>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public GetCleanwatchIdCommandHanders(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据编号显示清洗记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<CleanwatchItemDto>>> Handle(GetCleanwatchIdCommands request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<CleanwatchItemDto>> result = new ApiResult<List<CleanwatchItemDto>>();
                result.Code = ApiEnums.查询成功;
                result.Msg = "查询成功";
                var list = db.Queryable<Cleanwatch, CleanwatchItemed, ItemTable>((o, i, c) => new JoinQueryInfos(JoinType.Inner, o.CleaningId == i.CleaningId, JoinType.Inner, i.ItemId == c.ItemId)).Where(o => o.IsDelete == false && o.CleaningId == request.CleaningId).Select((o, i, c) => new CleanwatchItemDto
                {
                    CleaningId = o.CleaningId,
                    Cleaningequipment = o.Cleaningequipment,
                    Cleaningprocedure = o.Cleaningprocedure,
                    StateTime = o.StateTime,
                    Plannedduration = o.Plannedduration,
                    Washpeople = o.Washpeople,
                    Cleaningresult = o.Cleaningresult,
                    Cleaningtemperature = o.Cleaningtemperature,
                    Status = o.Status,
                    Itembarcode = o.Itembarcode,
                    IsDelete = o.IsDelete,
                    ItemId = i.ItemId,
                    Itemname = c.Itemname,
                    Itemnum = c.Itemnum,
                }).ToList();
                result.Data = mapper.Map<List<CleanwatchItemDto>>(list.ToList());
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
